package com.chandon.T02_functional;


import com.chandon.common.Cv;
import com.chandon.common.Role;

import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * * jdk1.8中提供的函数式接口与输入输出的要求
 * *
 * *      Supplier:1个输出
 * *          Supplier<T>
 * * ------------------------------
 * *      Consumer:1个输入
 * *          Consumer<T>
 * *      BiConsumer:2个输入
 * *          BiConsumer<T,U>
 * * ------------------------------
 * *      Function:1个输入1个输出，类型不同
 * *          Function<T, R>
 * *
 * *      BiFunction:2个输入,1个输出，类型不同
 * *          BiFunction<T, U, R>
 * * ------------------------------
 * *      UnaryOperator:1个输入1个输出，类型相同
 * *          UnaryOperator<T> extends Function<T, T>
 * *
 * *      BinaryOperator:2个输入1个输出，类型相同
 * *         BinaryOperator<T> extends BiFunction<T,T,T>
 *
 * @author Dong
 * @version 1.0
 * @date 2021-04-14 1:57
 */

public class T02_TestConsumer {

    public static void main(String[] args) {

        Cv cv0 = Cv.builder().age(17).name("asamiyui").scope(10).build();


        Role role0 = Role.builder().cv(cv0).name("yui").build();

        Supplier<Role> roleSupplier = Role::new;
        Role role1 = roleSupplier.get();


/*consumer没有返回值，所以不能使用Supplier的方式写有return的代码块

        Consumer<Role> consumer = role ->{return role0;};
        Consumer<Role> consumer = role ->role0;
*/
        // getCv有返回值可正常调用，但是consumer是接收不到的
        Consumer<Role> consumer3 = role -> role0.getCv();

        // 方法引用简写，不可调用有返回值的方法
//        Consumer<Role> consumer4 = role0::getCv;

        Consumer<Role> consumer5 = role -> {
            role.setName("asa");
            role.setCv(cv0);
            System.out.println("consumer:" + role);
        };


        consumer3.accept(role1);
        consumer3.accept(role0);

        consumer5.accept(role1);
        consumer5.accept(role0);



    }

}
